<?php


namespace App\Models;


use App\User;
use Illuminate\Support\Facades\DB;

class StoreDeal extends BaseModel
{
    protected $table = 'store_deal';

    public static function getRecordByDealTime($store_id, $date, $is_array = false)
    {
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date],
        ];
        $model = self::where($where)->first();
        if($model && $is_array){
            return $model->toArray();
        }
        return $model;
    }

    public static function createRecord($input)
    {
        $where = [
            ['store_id', '=', $input['store_id']],
            ['deal_time', '=', $input['deal_time']],
        ];
        $cateModel = Category::find($input['store_id']);
        if (!$cateModel) {
            uexception('区域不存在');
        }
        $model = self::where($where)->first();
        if ($model) {
            uexception('该日期已添加');
        }
        $model = new self();
        $input['region_id'] = $cateModel['parent_id'];
        $model = self::createModel($model, $input);
        return $model;
    }


    public static function getSumPeople($model)
    {

        return $model?$model['try_num'] + $model['buy_num']:0;
    }

    public static function getDealByWhere($where)
    {
        $model = self::where($where)
            ->first();
        return $model;
    }

    public static function jindian($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date],
        ];
        $now_model = self::getDealByWhere($where);
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', uyestoday($date)],
        ];
        $yestoday_model = self::getDealByWhere($where);
        $now_sum_people = self::getSumPeople($now_model);
        $day_jindian = self::ringRate(self::getSumPeople($now_model), self::getSumPeople($yestoday_model));
        list($num1, $num2, $num3) = self::yejiliangRate($now_model);
        $sum = 0;
        if($now_model){
            $sum = $now_model['kaifa_into']+$now_model['ziran_into']+$now_model['jiesao_into'];
        }
        $data = [
            'sum_people' => $now_sum_people,
            'deal' => $now_model?$now_model->toArray():'',
            'day_jindian' => $day_jindian,
            'week_jindian' => self::bossjinrate($store_id, uweeek($date), ulast_weeek($date)),
            'month_jindian' => self::bossjinrate($store_id, umonth($date), ulast_month($date)),
            'day_list' =>  self::dayList($store_id, $date ,1),
            'week_list' => self::dayList($store_id, $date ,2),
            'month_list' => self::dayList($store_id, $date ,3),
            'chartData' => [
                'sum' => $sum,
                'series' => [
                    ['name' => '入口','data' => $now_model?$now_model['kaifa_into']:0],
                    ['name' => '中间','data' => $now_model?$now_model['ziran_into']:0],
                    ['name' => '出口','data' => $now_model?$now_model['jiesao_into']:0],
                ]
            ],
            'yuan' => [
                ['title' => '入口', 'name' => '截流/开发', 'color' => '#F83A01', 'num' => $num1],
                ['title' => '中间', 'name' => '自然进店', 'color' => '#F83A01', 'num' => $num2],
                ['title' => '出口', 'name' => '邀约/跟踪/介绍', 'color' => '#F83A01', 'num' => $num3],
            ],
        ];

        return $data;
    }

    public static function bossjinrate($user_id, $date = '', $last_date = '')
    {
        $field = self::selectField();
        $where = [
            ['user_id', '=', $user_id],
        ];
        $sumField = ['sum_kaifa_into', 'sum_ziran_into', 'sum_jiesao_into'];
        return self::jindianwhereRate($where, $field,$sumField, $date, $last_date);
    }

    public static function jieliuRate($deal)
    {
        $zhun_vip = 0;
        $vip = 0;
        $zhunzhi_vip = 0;
        if($deal){
            $sum = $deal['kaifa_into']+$deal['ziran_into']+$deal['jiesao_into'];
            $zhun_vip = self::roundRingRate($deal['kaifa_into'],$sum);
            $vip = self::roundRingRate($deal['ziran_into'],$sum);
            $zhunzhi_vip = self::roundRingRate($deal['jiesao_into'],$sum);
        }
        return [$zhun_vip, $vip, $zhunzhi_vip];
    }

    //人环比计算
    public static function calcPeopleRate($store_id, $date, $last_date)
    {
        $where = [
            ['user_id', '=', $store_id],
        ];
        $now_model =  self::getDealByDateWhere($where, $date);
        $last_model = self::getDealByDateWhere($where, $last_date);
        return BaseModel::ringRate(
            self::getSumPeople($now_model),
            self::getSumPeople($last_model)
        );
    }

    public static function getDealJinByDateWhere($where,$date)
    {
        $model = self::where($where)
            ->whereBetween('deal_time', $date)
            ->first();
        return $model;
    }

    public static function getDealByDateWhere($where,$date)
    {
        $model = self::where($where)
            ->whereBetween('deal_time', $date)
            ->first();
        return $model;
    }

    public static function dayList($store_id, $date, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        foreach($now_days as $key => $val){
            $now_where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $now_model = self::getDealByWhere($now_where);
            $sum_array[] = self::getSumPeople($now_model);
        }
        $new_data = self::oneLine($now_days, $sum_array);
        return $new_data;
    }

    public static function sumYeji($model)
    {
        return $model?$model['future_vip_deal'] + $model['vip_deal'] + $model['deposit_vip_del']:0;
    }

    public static function yeji($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date]
        ];
        $model = self::getDealByWhere($where);
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', uyestoday($date)]
        ];
        $yestoryModel = self::getDealByWhere($where);
        $day_huanbi = BaseModel::ringRate(self::sumYeji($model), self::sumYeji($yestoryModel));
        list($zhun_vip_rate, $vip, $chuzhu_vip) = self::yejiliangRate($model);
        $data = [
            'today_sum_yeji' => self::sumYeji($model),
            'yestoday_sum_yeji' => self::sumYeji($yestoryModel),
            'deal' => $model?$model->toArray():'',
            'yestoday_deal' => $yestoryModel?$yestoryModel->toArray():'',
            'day_huanbi'=> $day_huanbi,
            'week_huanbi' => self::calcYejiRate($store_id, uweeek($date), ulast_weeek($date)),
            'month_huanbi' => self::calcYejiRate($store_id, umonth($date), ulast_month($date)),
            'day_list' => self::yejiDayList($store_id, $date, 1),
            'week_list' => self::yejiDayList($store_id, $date, 2),
            'month_list' => self::yejiDayList($store_id, $date, 3),
            'yejizhanbi' => self::tiaoxing($zhun_vip_rate, $vip,$chuzhu_vip),
        ];
        return $data;
    }

    public static function yejiliangRate($deal)
    {
        $zhun_vip = 0;
        $vip = 0;
        $zhunzhi_vip = 0;
        if($deal){
            $sum = $deal['future_vip_deal']+$deal['vip_deal']+$deal['deposit_vip_del'];
            $zhun_vip = self::roundRingRate($deal['future_vip_deal'],$sum);
            $vip = self::roundRingRate($deal['vip_deal'],$sum);
            $zhunzhi_vip = self::roundRingRate($deal['deposit_vip_del'],$sum);
        }
        return [$zhun_vip, $vip, $zhunzhi_vip];
    }


    public static function yejiDayList($store_id, $date, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        $tongbi = [];
        foreach($now_days as $key => $val){
            $now_where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $now_model = self::getDealByWhere($now_where);
            $last = date("Y-m-d", strtotime("-1 years",strtotime($val)));
            $last_where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $last],
            ];
            $last_model = self::getDealByWhere($last_where);
            $sum_array[] = !empty($now_model['today_sum_yeji'])?$now_model['today_sum_yeji']:0;
            $tongbi[] = self::ringRate(self::sumYeji($now_model), self::sumYeji($last_model));
        }
        $new_data = self::twoLine($now_days, $tongbi, $sum_array);
        return $new_data;
    }

    //业绩环比计算
    public static function calcYejiRate($store_id, $date, $last_date)
    {
        $where = [
            ['user_id', '=', $store_id],
        ];
        $now_model =  self::getDealByDateWhere($where, $date);
        $last_model = self::getDealByDateWhere($where, $last_date);
        return BaseModel::ringRate(
            self::getSumPeople($now_model),
            self::getSumPeople($last_model)
        );
    }

    //
    public static function huiyuan($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date],
        ];
        $model = self::getDealByWhere($where);
        $data = [
            'deal' => $model?$model->toArray():'',
            'sum_huiyuan' => self::sumHuiyuan($model),
            'day_list' => self::dayHuiYuanList($store_id, $date, 1),
            'week_list' => self::dayHuiYuanList($store_id, $date, 2),
            'month_list' => self::dayHuiYuanList($store_id, $date, 3),
        ];
        return $data;
    }

    public static function dayHuiYuanList($store_id, $date, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        foreach($now_days as $val){
            $where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $model = self::getDealByWhere($where);
            $sum_array[] = self::sumHuiyuan($model);
        }
        $new_data = self::oneLine($now_days, $sum_array);
        return $new_data;
    }

    public static function sumHuiyuan($model)
    {
        if(!$model){
            return 0;
        }
        return $model['huoke'] + $model['ziran'] + $model['jiesao'];
    }

    public static function yuangong($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date],
        ];
        $model = self::getDealByWhere($where);
        $data = [
            'deal' => $model?$model->toArray():'',
            'sum_yuangong' => User::countByStoreId($store_id),
            'day_list' => self::dayYuangong($store_id, $date,1),
            'week_list' => self::dayYuangong($store_id, $date,2),
            'month_list' => self::dayYuangong($store_id, $date,3),
        ];
        return $data;
    }

    public static function dayYuangong($store_id, $date, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        foreach($now_days as $val){
            $where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $model = self::getDealByWhere($where);
            $sum_array[] = self::sumHuiyuan($model);
        }
        $new_data = self::oneLine($now_days, $sum_array);
        return $new_data;
    }

    //
    public static function sanda($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date]
        ];
        $model = self::getDealByWhere($where);
        $where = [
            ['store_id', '=', $store_id],
            ['deal_time', '=', uyestoday($date)]
        ];
        $yestoryModel = self::getDealByWhere($where);

        $data = [
            'zhun_vip_num' => self::zhunRate($model, $yestoryModel,'san_vip_zhu'),
            'vip_num' => self::zhunRate($model, $yestoryModel,'san_vip'),
            'chuzhi_vip_num' => self::zhunRate($model, $yestoryModel,'san_vip_chu'),
            'zhun_vip_list' => self::daySandaList($store_id, $date, 'san_vip_zhu'),
            'vip_list' => self::daySandaList($store_id, $date, 'san_vip'),
            'chuzhi_vip_list' => self::daySandaList($store_id, $date, 'san_vip_chu'),
        ];
        return $data;
    }

    public static function zhunRate($model, $yestoryModel, $field = '')
    {
       if(empty($model) || $yestoryModel){
           return 0;
       }
       return self::ringRate($model[$field], $yestoryModel[$field]);
    }

    public static function daySandaList($store_id, $date, $field = '')
    {
        $now_days = get_days($date, 6);
        $sum_array = [];
        foreach($now_days as $val){
            $where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $model = self::getDealByWhere($where);
            $sum_array[] = $model?(int)$model[$field]:0;
        }
        $new_data = self::oneLine($now_days, $sum_array);
        return $new_data;
    }

    public static function yueding($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', $date]
        ];
        $model = self::getDealByWhere($where);

        $where = [
            ['user_id', '=', $store_id],
            ['deal_time', '=', uyestoday($date)]
        ];
        $yestoryModel = self::getDealByWhere($where);

        $data = [
            'deal' => $model?$model->toArray():'',
            'kaifa_num' => self::modelField($model, 'kaifa_into'),
            'sum_yeji' => self::sumYueDing($model),
            'new_sum_yeji' => $model?$model['future_vip_deal']:0,
            'old_sum_yeji' => $model?$model['vip_deal']+$model['deposit_vip_del']:0,
            'xin_rate' => self::zhunRate($model, $yestoryModel,'kaifa_into'),
            'zong_rate' => self::ringRate(self::sumYueDing($model), self::sumYueDing($yestoryModel)),
            'day_list' => self::yuedingDayList($store_id, $date, 1),
            'week_list' => self::yuedingDayList($store_id, $date, 2),
            'month_list' => self::yuedingDayList($store_id, $date, 3),
        ];
        return $data;
    }

    public static function sumYueDing($model)
    {
        if(!$model){
            return 0;
        }
        return $model['future_vip_deal'] + $model['vip_deal'] + $model['deposit_vip_del'];
    }

    public static function yuedingDayList($store_id, $date, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        $array = [];
        foreach($now_days as $key => $val){
            $now_where = [
                ['user_id', '=', $store_id],
                ['deal_time', '=', $val],
            ];
            $now_model = self::getDealByWhere($now_where);

            $sum_array[] = !empty($now_model['kaifa_into'])?$now_model['kaifa_into']:0;
            $array[] = !empty($now_model['jiesao_into'])?$now_model['jiesao_into']:0;;
        }
        $new_data = self::twoLine($now_days, $sum_array, $array,'新客户','老客户');
        return $new_data;
    }

    public static function zhizhao($user, $date)
    {
        $store_id = $user['id'];
        $where = [
            ['user_id', '=', $store_id],
        ];
        $model = self::getDealListByDateWhere($where, uweeek($date));
        $week_zhao_sum = 0;
        $week_ru_sum = 0;
        foreach ($model as $val){
            $week_zhao_sum += $val['zhao_num'];
            $week_ru_sum += $val['ru_num'];
        }
        $now_date[] = $date;
        $data = [
            'week_ru_sum' => $week_zhao_sum,
            'week_zhao_sum' => $week_ru_sum,
            'day_list' => self::twoLine($now_date, [$week_ru_sum],[$week_zhao_sum], '总入职人数', '总招聘人数'),
        ];
        return $data;
    }

    public static function getDealListByDateWhere($where,$date)
    {
        $model = self::where($where)
            ->whereBetween('deal_time', $date)
            ->get()
            ->toArray();
        return $model;
    }

    public static function tongji($user,$date)
    {
        $where = [
            ['store_id', '=', $user['store_id']],
            ['deal_time', '=', $date],
        ];
        $field = 'sum(future_vip_deal) as sum_zhun_vip,sum(vip_deal) as sum_vip,sum(deposit_vip_del) as chuzhi_vip,sum(full_5000_num) as sum_5000,sum(buy_num) as sum_buy_num';
        $model = self::where($where)
            ->select(DB::raw($field))
            ->first()
            ->toArray();

        $where = [
            ['store_id', '=', $user['store_id']],
        ];
        $monthModel = self::where($where)
            ->whereBetween('deal_time', umonth($date))
            ->select(DB::raw($field))
            ->first()
            ->toArray();
        $data = [
            'day_sum' => self::sumTongji($model),
            'day_sum_500' => $model?$model['sum_5000']:0,
            'day_yeji' => $model?$model['sum_buy_num']:0,
            'month_sum' => self::sumTongji($monthModel),
            'month_sum_500' => $monthModel?$monthModel['sum_5000']:0,
            'month_yeji' => $monthModel?$monthModel['sum_buy_num']:0,
            'rank_list' => self::storeList($user, $date),
        ];
        return $data;
    }

    public static function sumTongji($model)
    {
        return $model ? $model['sum_zhun_vip'] + $model['sum_vip']+ $model['chuzhi_vip']:0;
    }

    public static function storeList($user, $date)
    {
        $list = Cate::getSotreListById($user['region_id']);
        foreach($list as $key=>&$val){
            $field = 'sum(future_vip_deal) as sum_zhun_vip,sum(vip_deal) as sum_vip,sum(deposit_vip_del) as chuzhi_vip,sum(full_5000_num) as sum_5000';
            $where = [
                ['store_id', '=', $val['id']],
                ['deal_time', '=', $date],
            ];
            $model = self::where($where)
                ->select(DB::raw($field))
                ->first()
                ->toArray();
            $val['sum_xiaoshoue'] = self::sumTongji($model);
            $val['sum_yeji'] = $model?$model['sum_5000']:0;
            $val['ranking'] = $key+1;
            $val['user'] = User::getSimpleUserById($val['user_id'],true);
        }
        array_multisort(array_column($list,'sum_xiaoshoue'),SORT_DESC,$list);
        return $list;
    }

    public static function paramsVliadte($input, $field)
    {
        if(empty($input[$field])){
            uexception($field.'参数必传');
        }
    }

    public static function userWhere($input)
    {
        $user = auth()->user();
        if($user['user_type'] == 2){
            self::paramsVliadte($input, 'store_id');
            $where = [
                ['store_id', '=', $input['store_id']],
            ];

        } else {
            self::paramsVliadte($input, 'region_id');
            $where = [
                ['region_id', '=', $input['region_id']],
            ];
        }
        return $where;
    }

    public static function getDealFieldByWhere($where, $field)
    {
        $model = self::where($where)
            ->select(DB::raw($field))
            ->first()
            ->toArray();
        return $model;
    }

    public static function sumField($model, $field)
    {
        if(!$model){
            return 0;
        }
        $sum = 0;
        foreach($field as $val){
            $sum += $model[$val];
        }
        return $sum;
    }
    public static function getDealFieldByDateWhere($where,$field,$date)
    {
        $model = self::where($where)
            ->select(DB::raw($field))
            ->whereBetween('deal_time', $date)
            ->first()
            ->toArray();
        return $model;
    }
    public static function selectField()
    {
        $field = 'sum(try_num) as sum_try_num,sum(buy_num) as sum_buy_num,sum(kaifa_into) as sum_kaifa_into,sum(ziran_into) as sum_ziran_into,sum(jiesao_into) as sum_jiesao_into';
        return $field;
    }

    public static function actModel($input, $date, $field,$flag = false)
    {
        $where = self::userWhere($input);
        $now_where[] = ['deal_time', '=', $date];
        $now_where = array_merge($where,$now_where);
        $nowModel = self::getDealFieldByWhere($now_where, $field);

        if(!empty($flag)){
            $lastModel = [];
        } else {
            $last_where[] = ['deal_time', '=', uyestoday($date)];
            $last_where = array_merge($where,$last_where);
            $lastModel = self::getDealFieldByWhere($last_where, $field);
        }

        return [$nowModel, $lastModel];
    }

    public static function bossjindian($input, $date)
    {
        $field = self::selectField();
        list($nowModel, $lastModel) = self::actModel($input, $date, $field);
        $sumField = ['sum_kaifa_into', 'sum_ziran_into', 'sum_jiesao_into'];
        $sum = 0;
        if($nowModel){
            $sum = $nowModel['sum_kaifa_into']+$nowModel['sum_ziran_into']+$nowModel['sum_jiesao_into'];
        }
        $data = [
            'jinri_sum'=>self::sumJindian($nowModel),
            'try_sum'=>$nowModel?$nowModel['sum_try_num']:0,
            'buy_sum'=>$nowModel?$nowModel['sum_buy_num']:0,
            'day_rate'=>self::ringRate(self::sumField($nowModel,$sumField), self::sumField($lastModel,$sumField)),
            'week_rate'=>self::jindianWeekRate($input, $field, $sumField, uweeek($date), ulast_weeek($date)),
            'month_rate'=>self::jindianWeekRate($input, $field, $sumField, umonth($date), ulast_month($date)),
            'day_list'=>self::countDayList($input, $date, $sumField,1),
            'week_list'=>self::countDayList($input, $date, $sumField,2),
            'month_list'=>self::countDayList($input, $date, $sumField,3),
            'chartData' => [
                'sum' => $sum,
                'series' => [
                    ['name' => '入口','data' => $nowModel?(int)$nowModel['sum_kaifa_into']:0],
                    ['name' => '中间','data' => $nowModel?(int)$nowModel['sum_ziran_into']:0],
                    ['name' => '出口','data' => $nowModel?(int)$nowModel['sum_jiesao_into']:0],
                ]
            ],
            'yuan' => [
                ['title' => '入口', 'name' => '截流/开发', 'color' => '#F8C91F', 'num' => self::rate100($nowModel, $sumField, 'sum_kaifa_into')],
                ['title' => '中间', 'name' => '自然进店', 'color' => '#47C0F8', 'num' => self::rate100($nowModel, $sumField, 'sum_ziran_into')],
                ['title' => '出口', 'name' => '邀约/跟踪/介绍', 'color' => '#F84611', 'num' => self::rate100($nowModel, $sumField, 'sum_jiesao_into')],
            ],
        ];
        return $data;
    }

    public static function rate100($model, $sumField, $field)
    {
        if(!$model){
            return 0;
        }
        $sum = self::sumField($model, $sumField);
        return self::roundRingRate($model[$field], $sum);
    }

    public static function jindianWeekRate($input, $selecField, $sumField, $date = '', $last_date = '')
    {
        $where = self::userWhere($input);
        return self::jindianwhereRate($where, $selecField, $sumField, $date, $last_date);
    }

    public static function jindianwhereRate($where, $selecField, $sumField, $date = '', $last_date = '')
    {
        $now_model = self::getDealFieldByDateWhere($where, $selecField, $date);
        $last_model = self::getDealFieldByDateWhere($where, $selecField, $last_date);
        $rate1 = self::sumField($now_model, $sumField);
        $rate2 = self::sumField($last_model, $sumField);
        return self::ringRate($rate1, $rate2);
    }

    public static function sumJindian($model)
    {
        return $model?$model['sum_try_num']+$model['sum_buy_num']:0;
    }

    public static function countDayList($input, $date,$sumField,$type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        foreach($now_days as $key => $val){
            $now_where = self::userWhere($input);
            $now_where[] = ['deal_time', '=', $val];
            $now_model = self::getDealFieldByWhere($now_where, self::selectField());
            $sum_array[] = self::sumField($now_model, $sumField);
        }
        $new_data = self::oneLine($now_days, $sum_array);
        return $new_data;
    }

    public static function bossyeji($input, $date)
    {
        $field = 'sum(future_vip_deal) as sum_future_vip_deal,sum(vip_deal) as sum_vip_deal,sum(deposit_vip_del) as sum_deposit_vip_del, sum(full_5000_num) as sum_full_5000_num';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field);
        $sumField = ['sum_future_vip_deal', 'sum_vip_deal', 'sum_deposit_vip_del'];
        $data = [
            'today_yeji_sum' => self::sumField($nowModel, $sumField),
            'yestoday_yeji_sum' => self::sumField($lastModel, $sumField),
            'sum_full_5000' => self::modelField($nowModel, 'sum_full_5000_num'),
            'zhun_vip_sum' => self::modelField($nowModel, 'sum_future_vip_deal'),
            'vip_sum' => self::modelField($nowModel, 'sum_vip_deal'),
            'chuzhi_vip_sum' => self::modelField($nowModel, 'sum_deposit_vip_del'),
            'day_rate' => self::ringRate(self::sumField($nowModel,$sumField), self::sumField($lastModel,$sumField)),
            'week_rate' => self::jindianWeekRate($input, $field,$sumField, uweeek($date), ulast_weeek($date)),
            'month_rate' => self::jindianWeekRate($input, $field,$sumField, umonth($date), ulast_month($date)),
            'day_list' => self::bossyejiDayList($input,$date, $field,$sumField,1),
            'week_list' => self::bossyejiDayList($input,$date, $field,$sumField,2),
            'month_list' => self::bossyejiDayList($input,$date, $field,$sumField,3),
            'yejizhanbi' => self::tiaoxing(
                self::rate100($nowModel, $sumField, 'sum_future_vip_deal'),
                self::rate100($nowModel, $sumField, 'sum_vip_deal'),
                self::rate100($nowModel, $sumField, 'sum_deposit_vip_del')
            ),
        ];
        return $data;
    }

    public static function bossyejiDayList($input, $date,$selectField,$sumField, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        $tongbi = [];
        foreach($now_days as $key => $val){
            $now_where = self::userWhere($input);
            $now_where[] = ['deal_time', '=', $val];
            $now_model = self::getDealFieldByWhere($now_where, $selectField);

            $sum_array[] = self::sumField($now_model, $sumField);

            $last = date("Y-m-d", strtotime("-1 years",strtotime($val)));
            $last_where = self::userWhere($input);
            $last_where[] = ['deal_time', '=', $last];
            $last_model = self::getDealFieldByWhere($now_where, $selectField);
            $tongbi[] = self::ringRate(
                self::sumField($now_model, $sumField),
                self::sumField($last_model, $sumField)
            );
        }
        $new_data = self::twoLine($now_days, $tongbi, $sum_array);
        return $new_data;
    }


    public static function modelField($model, $field)
    {
        return $model?(int)$model[$field]:0;
    }

    public static function huiyuanzl($input, $date)
    {
        $field = 'sum(huoke) as sum_huoke,sum(ziran) as sum_ziran,sum(jiesao) as sum_jiesao';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field, true);
        $sumField = ['sum_huoke', 'sum_ziran', 'sum_jiesao'];
        $data = [
            'sum_huoke' => self::modelField($nowModel,'sum_huoke'),
            'sum_zhuanhua' => self::modelField($nowModel,'sum_ziran'),
            'sum_jieshao' => self::modelField($nowModel,'sum_jiesao'),
            'sum_zong' => self::sumField($nowModel,$sumField),
            'day_list' => self::bossyejiDayList1($input,$date, $field,$sumField,1),
            'week_list' => self::bossyejiDayList1($input,$date, $field,$sumField,2),
            'month_list' => self::bossyejiDayList1($input,$date, $field,$sumField,3),
        ];
        return $data;
    }

    public static function yuangongzl($input, $date)
    {
        $field = 'sum(ru_num) as sum_ru_num';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field, true);
        $sumField = ['sum_ru_num'];
        $user = auth()->user();
        $sum_zong = $user['user_type'] == 2?User::countByRegionId($user['region_id']):User::count();
        $data = [
            'sum_ru_num' => self::modelField($nowModel,'sum_ru_num'),
            'sum_zong' => $sum_zong,
            'day_list' => self::bossyejiDayList1($input,$date, $field,$sumField,1),
            'week_list' => self::bossyejiDayList1($input,$date, $field,$sumField,2),
            'month_list' => self::bossyejiDayList1($input,$date, $field,$sumField,3),
        ];
        return $data;
    }

    public static function sandasy($input, $date)
    {
        $field = 'sum(san_vip_zhu) as sum_san_vip_zhu,sum(san_vip) as sum_san_vip,sum(san_vip_chu) as sum_san_vip_chu';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field);
        $sumField = ['sum_san_vip_zhu', 'sum_san_vip', 'sum_san_vip_chu'];
        $data = [
            'zhun_vip_rate' => self::ringRate(self::modelField($nowModel,'sum_san_vip_zhu'),self::modelField($lastModel,'sum_san_vip_zhu')),
            'vip_rate' => self::ringRate(self::modelField($nowModel,'sum_san_vip'),self::modelField($lastModel,'sum_san_vip')),
            'chuzhi_vip_rate' => self::ringRate(self::modelField($nowModel,'sum_san_vip_chu'),self::modelField($lastModel,'sum_san_vip_chu')),
            'zhun_vip_line' => self::bossyejiDayList1($input,$date, $field,['sum_san_vip_zhu'],2),
            'vip_line' =>  self::bossyejiDayList1($input,$date, $field,['sum_san_vip'],2),
            'chuzhi_vip_line' =>  self::bossyejiDayList1($input,$date, $field,['sum_san_vip_chu'],2),
        ];
        return $data;
    }

    //1条柱线图
    public static function bossyejiDayList1($input, $date,$selectField,$sumField, $type = 1)
    {
        $now_days = self::lineDays($date, $type);
        $sum_array = [];

        foreach($now_days as $key => $val){
            $now_where = self::userWhere($input);
            $now_where[] = ['deal_time', '=', $val];
            $now_model = self::getDealFieldByWhere($now_where, $selectField);
            $sum_array[] = self::sumField($now_model, $sumField);
        }
        $new_data = self::oneLine($now_days,$sum_array);
        return $new_data;
    }

    public static function bossyueding($input, $date)
    {
        $field = 'sum(kaifa_into) as sum_kaifa_into,sum(future_vip_deal) as sum_future_vip_deal,sum(vip_deal) as sum_vip_deal,sum(deposit_vip_del) as sum_deposit_vip_del,sum(san_vip) as sum_san_vip, sum(san_vip_chu) as sum_san_vip_chu';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field);
        $sumField = ['sum_future_vip_deal', 'sum_vip_deal', 'sum_deposit_vip_del'];
        self::bossyuedingdaylist($input, $date,$field, $sumField, 1);
       // die();
        $data = [
            'kaifa_new' => self::modelField($nowModel, 'sum_kaifa_into'),
            'new_deal' => self::modelField($nowModel, 'sum_future_vip_deal'),
            'old_buy' => self::sumField($nowModel, ['sum_vip_deal','sum_deposit_vip_del']),
            'zong_yejie' => self::sumField($nowModel, $sumField),
            'zong_kaifa_rate' => self::ringRate(self::modelField($nowModel,'sum_kaifa_into'),self::modelField($lastModel,'sum_kaifa_into')),
            'zong_yejie_rate' => self::ringRate(self::sumField($nowModel,$sumField), self::sumField($lastModel, $sumField)),
            'day_list' => self::bossyuedingdaylist($input, $date, $field, $sumField,1),
            'week_list' => self::bossyuedingdaylist($input, $date, $field, $sumField,2),
            'month_list' => self::bossyuedingdaylist($input, $date, $field, $sumField,3),
        ];
        return $data;
    }

    public static function bossyuedingdaylist($input, $date,$selectField,$sumField, $type = 1)
    {

        $now_days = self::lineDays($date, $type);
        $sum_array = [];
        $array = [];
        foreach($now_days as $key => $val){
            list($nowModel, $lastModel) = self::actModel($input, $val, $selectField);
            $sum_array[] = self::modelField($nowModel,'sum_kaifa_into');
            $array[] = self::sumField($nowModel,['sum_san_vip','sum_san_vip_chu']);
        }
        $new_data = self::twoLine($now_days, $sum_array, $array,'新客户','老客户');
        return $new_data;
    }

    public static function bosszhizhao($input, $date)
    {
        $field = 'sum(zhao_num) as sum_zhao_num,sum(ru_num) as sum_ru_num';
        list($nowModel, $lastModel) = self::actModel($input, $date, $field);
        $data = [
            'week_zhaopin' => self::modelField($nowModel,'sum_zhao_num'),
            'sun_ruzhi' => self::modelField($nowModel,'sum_ru_num'),
            'day_list' => self::bossyejiDayList1($input,$date, $field,['sum_ru_num'],1),
            'week_list' => self::bossyejiDayList1($input,$date, $field,['sum_ru_num'],2),
            'month_list' => self::bossyejiDayList1($input,$date, $field,['sum_ru_num'],3),
        ];
        return $data;
    }

    public static function zqtongji($user, $date)
    {
        $where = [
            ['deal_time', '=', $date],
        ];
        $field = 'sum(future_vip_deal) as sum_zhun_vip,sum(vip_deal) as sum_vip,sum(deposit_vip_del) as chuzhi_vip,sum(full_5000_num) as sum_5000,sum(buy_num) as sum_buy_num';
        $model = self::where($where)
            ->select(DB::raw($field))
            ->first()
            ->toArray();
        $monthModel = self::whereBetween('deal_time', umonth($date))
            ->select(DB::raw($field))
            ->first()
            ->toArray();
        $data = [
            'day_sum' => self::sumTongji($model),
            'day_sum_500' => self::modelField($model,'sum_5000'),
            'day_yeji' => self::modelField($model,'sum_buy_num'),
            'month_sum' => self::sumTongji($monthModel),
            'month_sum_500' => self::modelField($monthModel,'sum_5000'),
            'month_yeji' =>self::modelField($monthModel,'sum_buy_num'),
            'rank_list' => self::zhanqulist($user, $date),
        ];
        return $data;
    }

    public static function zhanqulist($user, $date)
    {
        $list = Cate::where('parent_id', 0)
            ->get()
            ->toArray();
        foreach($list as $key=>&$val){
            $field = 'sum(future_vip_deal) as sum_zhun_vip,sum(vip_deal) as sum_vip,sum(deposit_vip_del) as chuzhi_vip,sum(full_5000_num) as sum_5000';
            $where = [
                ['region_id', '=', $val['id']],
                ['deal_time', '=', $date],
            ];
            $model = self::where($where)
                ->select(DB::raw($field))
                ->first()
                ->toArray();
            $val['sum_xiaoshoue'] = self::sumTongji($model);
            $val['sum_yeji'] = $model?(int)$model['sum_5000']:0;
            $val['ranking'] = $key+1;
            $val['user'] = User::getSimpleUserById($val['user_id'],true);
        }
        array_multisort(array_column($list,'sum_xiaoshoue'),SORT_DESC,$list);
        return $list;
    }
}